home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / util / cli / cc.lha / CC.C < prev    next >
C/C++ Source or Header  |  1996-12-03  |  3KB  |  150 lines

  1. /*CC v.1.0 - Numeric standart converter by Adam Kubiczek
  2.                            (c) 1996 by Adam Kubiczek
  3. Usage: CC fxxxx [std]
  4.     where f is number of system and xxxx is number, avaiable systems:
  5.         $ - hex (hexdecimal)
  6.         & - oct
  7.         % - bin (binary)
  8.         # - dec (decimal) (also without # number is decimal i.g. CC 512 is the same CC #512)
  9.  
  10. or Usage: CC !aaxxxx [std]
  11.     where ! is char '!', xxxx is number and aa is system of number, i.g.:
  12.         CC !161ff is the same CC $1ff
  13.         CC !10512 is the same CC #512 or CC 512
  14.         CC !021111 is the same CC %1111
  15.         etc.
  16.  
  17. [std] is a system to which the number will converted. It's not requeired, if it not
  18. exists program will print result in DEC,HEX,OCT and BIN system.
  19.  
  20. (sorry 4 my english!)
  21. */
  22.  
  23. #define NO_PC
  24.  
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #ifdef NO_PC
  28.     #include <proto/dos.h>
  29. #else
  30.     #include <STDIO.h>
  31. #endif
  32.  
  33.  
  34. char  Wynik[32];
  35. unsigned char VER[]="$VER: CalcConv V1.0 (01.12.96) by Adam Kubiczek";
  36.  
  37. unsigned long pot(int a, short j)
  38. {
  39.     short i;
  40.     unsigned long w=1;
  41.     for (i=1;i<=j;i++) w=w*a;
  42.     return (w);
  43. }
  44.  
  45. unsigned long Conv2Dec(char *liczba)
  46. {
  47.   short i,j=0,k=1;
  48.     int system;
  49.     unsigned long wynik=0,m=0;
  50.  
  51.     switch (liczba[0]) {
  52.         case '#': system=10;break;
  53.         case '$': system=16;break;
  54.         case '&': system=8;break;
  55.         case '%':    system=2;break;
  56.         case '!': {system=Conv2Dec( (strncpy (Wynik,liczba,3))+1);k=3;break;}
  57.         default : {system=10;k=0;break;}
  58.     }
  59.  
  60.     for (i=strlen(liczba)-1;i>=k;i--) {
  61.         m=liczba[i];
  62.         m=(m<58 ? m-48 : m-87);
  63.         wynik=wynik+(m*pot(system,j));
  64.         j++;
  65.     }
  66.  
  67.     return(wynik);
  68. }
  69.  
  70. void Conv2Other(unsigned long liczba, short system)
  71. {
  72.      int i=0;
  73.     unsigned long r,a;
  74.     char b;
  75.  
  76.   strnset(Wynik,0,31);
  77.  
  78.     do {
  79.  
  80.         a=liczba/system;
  81.         r=liczba-(a*system);
  82.         liczba=a;
  83.         Wynik[i]=(r<10 ? r+48 : r+87);
  84.         i++;
  85.  
  86.     } while (liczba!=0);
  87.  
  88.     for (a=0;a<(i/2);a++) {
  89.     b=Wynik[a];
  90.         Wynik[a]=Wynik[i-1-a];
  91.         Wynik[i-1-a]=b;
  92.     }
  93. }
  94.  
  95.  
  96. main(int argc, char *argv[])
  97. {
  98.     unsigned long decliczba;
  99.     unsigned char usersystem=0;
  100.  
  101. #ifdef NO_PC
  102.     if (argc<2) {PutStr("Usage: CC cXXXX [std], where c is #,$,% or &.\n");return(5);}
  103. #else
  104.     if (argc<2) {printf("Usage: CC cXXXX [std]\n");return(5);}
  105. #endif
  106.  
  107.     if (argc=3) usersystem=atoi(argv[2]);
  108.  
  109.     strlwr(argv[1]);
  110.     decliczba=Conv2Dec(argv[1]);
  111.  
  112. #ifdef NO_PC
  113.     Conv2Other(decliczba,10);
  114.     PutStr("Dec:");
  115.     PutStr(Wynik);
  116.  
  117.     if (usersystem>1) {
  118.         Conv2Other(decliczba,usersystem);
  119.         PutStr("\nUser:");PutStr(Wynik);PutStr("\n");
  120.     } else {
  121.  
  122.         Conv2Other(decliczba,16);
  123.         PutStr("\nHex:");
  124.         PutStr(Wynik);
  125.  
  126.         Conv2Other(decliczba,8);
  127.         PutStr("\nOct:");
  128.         PutStr(Wynik);
  129.  
  130.         Conv2Other(decliczba,2);
  131.         PutStr("\nBin:");
  132.         PutStr(Wynik);
  133.         PutStr("\n");
  134.     }
  135.  
  136. #else
  137.   printf("Dec:%d",decliczba);            //poniewaû IBM nie ma systemu
  138.     printf("\nHex:%x",decliczba);        //to uûywamy standartowych funkcji C
  139.     printf("\nOct:%o",decliczba);        //dziëki czemu wydîuûamy program 6x
  140.  
  141.     Conv2Other(decliczba,2);
  142.     printf("\nBin:%s",Wynik);
  143.  
  144.     if (usersystem>1) {
  145.         Conv2Other(decliczba,usersystem);
  146.         printf("\nUser:%s\n",Wynik);
  147.     }
  148. #endif
  149.  
  150. }